<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  
  <title>WebCrawlers | H5教案</title>

  
  <meta name="author" content="John Doe">
  

  
  <meta name="description" content="4、爬虫cheerio是模仿jQuery方式实现的一个第三方爬虫解析插件，可以将文本像html一样通过选择器和筛选器将数据分离
123456789101112131415161718192021222324252627282930313233343536373839404142434445var h">
  

  
  
  <meta name="keywords" content="">
  

  <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <meta property="og:title" content="WebCrawlers"/>

  <meta property="og:site_name" content="H5教案"/>

  
  <meta property="og:image" content="/ericxie-book/favicon.ico"/>
  

  <link href="/ericxie-book/favicon.ico" rel="icon">
  <link rel="alternate" href="/ericxie-book/atom.xml" title="H5教案" type="application/atom+xml">
  <link rel="stylesheet" href="/ericxie-book/css/style.css" media="screen" type="text/css">
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
<div class="blog">
  <div class="content">

    <header>
  <div class="site-branding">
    <h1 class="site-title">
      <a href="/ericxie-book/">H5教案</a>
    </h1>
    <p class="site-description"></p>
  </div>
  <nav class="site-navigation">
    <ul>
      
    </ul>
  </nav>
</header>

    <main class="site-main posts-loop">
    <article>

  
    
    <h3 class="article-title"><span>WebCrawlers</span></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/26/WebCrawlers/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-26T01:49:21.000Z">
          2021-01-26
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="4、爬虫"><a href="#4、爬虫" class="headerlink" title="4、爬虫"></a>4、爬虫</h2><p>cheerio是模仿jQuery方式实现的一个第三方爬虫解析插件，可以将文本像html一样通过<br>选择器和筛选器将数据分离</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">var http=require(<span class="string">&quot;http&quot;</span>);</span><br><span class="line">var https=require(<span class="string">&quot;https&quot;</span>);</span><br><span class="line">var url=require(<span class="string">&quot;url&quot;</span>);</span><br><span class="line">var cheerio=require(<span class="string">&quot;cheerio&quot;</span>);</span><br><span class="line"><span class="keyword">function</span> createServer(fn)&#123;</span><br><span class="line">        http.createServer((req,res)=&gt;&#123;</span><br><span class="line">            res.writeHead(200,&#123;</span><br><span class="line">                <span class="string">&quot;content-type&quot;</span>:<span class="string">&quot;application/json;charset=utf-8&quot;</span>,</span><br><span class="line">                <span class="string">&quot;Access-Control-Allow-Origin&quot;</span>:<span class="string">&quot;*&quot;</span></span><br><span class="line">            &#125;)</span><br><span class="line">         <span class="built_in">return</span>  fn(&#123;req:req,res:res&#125;);</span><br><span class="line">        &#125;).listen(9000);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> createRequest(req,res)&#123;</span><br><span class="line">    <span class="built_in">return</span> new Promise((resolve,reject)=&gt;&#123;</span><br><span class="line">       var reqs=https.request(url.parse(<span class="string">&quot;https://maoyan.com:443/&quot;</span>),(reslute=&gt;&#123;</span><br><span class="line">            <span class="built_in">let</span> data=<span class="string">&quot;&quot;</span>;</span><br><span class="line">            reslute.on(<span class="string">&quot;data&quot;</span>,_data=&gt;data+=_data);</span><br><span class="line">            reslute.on(<span class="string">&quot;end&quot;</span>,<span class="function"><span class="title">function</span></span>()&#123;</span><br><span class="line">               resolve(&#123;req:req,res:res,data:data&#125;);</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;));</span><br><span class="line">        reqs.end();</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> filterData(data)&#123;</span><br><span class="line">    $=cheerio.load(data);</span><br><span class="line">    <span class="built_in">let</span> movieList=$(<span class="string">&quot;.movie-item&quot;</span>);</span><br><span class="line">    <span class="built_in">let</span> movies = []</span><br><span class="line">    movieList.each((index, value) =&gt; &#123;</span><br><span class="line">      movies.push(&#123;</span><br><span class="line">        title: $(value).find(<span class="string">&#x27;.movie-title&#x27;</span>).attr(<span class="string">&#x27;title&#x27;</span>),</span><br><span class="line">        score: $(value).find(<span class="string">&#x27;.movie-score i&#x27;</span>).text(),</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="built_in">return</span> movies;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">createServer(<span class="keyword">function</span>(&#123;req,res&#125;)&#123;</span><br><span class="line">  <span class="built_in">return</span>  createRequest(req,res);</span><br><span class="line">&#125;).<span class="keyword">then</span>(<span class="keyword">function</span>(&#123;req,res,data&#125;)&#123;</span><br><span class="line">    res.end(JSON.stringify(filterData(data)));</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>

  






    </main>

    <footer class="site-footer">
  <p class="site-info">
    Proudly powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and
    Theme by <a href="https://github.com/CodeDaraW/Hacker" target="_blank">Hacker</a>
    </br>
    
    &copy; 2021 John Doe
    
  </p>
</footer>
    
    
  </div>
</div>
</body>
</html>